
module.exports = app => {

    return class HomeController extends app.Controller {

        async index() {
            let forumIndex = await this.ctx.service.forum.forumIndex();
            let type = await this.ctx.service.forum.getAllType();
            await this.ctx.render('forum/index.js', {
                forumIndex,
                type
            });
        }


        /**
         * 社区分类
         * @returns {Promise<void>}
         */
        async category() {
            let forumTypeId = this.ctx.request.query.forumTypeId;
            let type = await this.ctx.service.forum.getAllType();

            let obj = {};

            for (let i = 0; i < type.length; i++){

                if (!forumTypeId && i == 0){
                    forumTypeId = type[i].id;
                }

                obj[type[i].id] = type[i];
            }

            await this.ctx.render('forum/category/index.js', {
                obj,
                forumTypeId
            });
        }

        async categoryList() {
            let forumTypeId = this.ctx.request.query.forumTypeId;
            let typeList = await this.ctx.service.forum.getTypeChildren({ forumTypeId });
            this.success(typeList)
        }


        async search() {
            let keyword = this.ctx.request.query.keyword;
            let data = await this.ctx.service.forum.search({ keyword });
            this.success(data)
        }



    };
};
